package com.open.capacity.paper.service.impl;

import com.open.capacity.paper.dao.*;
import com.open.capacity.paper.feign.UserFeignClient;
import com.open.capacity.paper.service.PaperRecordService;
import com.open.capacity.paper.vo.PaperRecordVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.*;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PaperRecordServiceImpl extends ServiceImpl<PaperRecordDao, PaperRecord> implements PaperRecordService {

    @Autowired
    private PaperRecordDao paperRecordDao;

    @Autowired
    private PaperRecordInfoDao paperRecordInfoDao;

    @Autowired
    private SuggestDao suggestDao;

    @Autowired
    private PaperDao paperDao;

    @Autowired
    private RateTypeDao rateTypeDao;

    @Resource
    private UserFeignClient userFeignClient;

    @Override
    public PageResult<PaperRecordVo> getPaperRecords(Map<String, Object> params) throws ServiceException {
        try {
            if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null) {
                PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
            }
            List<PaperRecord> list = paperRecordDao.findList(params);
            List<PaperRecordVo> recordVoList = list.stream().map(paperRecord -> {
                PaperRecordVo paperRecordVo = new PaperRecordVo();
                BeanUtils.copyProperties(paperRecord, paperRecordVo);
                SysUser user = userFeignClient.findUserById(paperRecord.getUserId());
                paperRecordVo.setWriteUser(user.getUsername());
                Paper paper = paperDao.selectById(paperRecord.getPaperId());
                paperRecordVo.setPaperTitle(paper.getTitle());
                RateType rateType = rateTypeDao.getRateType(paper.getRateTypeCode());
                paperRecordVo.setRateCode(rateType.getRateTypeName());
                return paperRecordVo;
            }).collect(Collectors.toList());
            PageInfo<PaperRecordVo> pageInfo = new PageInfo<>(recordVoList);
            PageInfo<PaperRecord> paperRecordInfo = new PageInfo<>(list);
            return PageResult.<PaperRecordVo>builder().code(0).count(paperRecordInfo.getTotal()).data(pageInfo.getList()).build();
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deletePaperRecord(Integer id,Long userId) throws ServiceException {
        try {
            paperRecordDao.deletePaperRecord(id,userId);
            int i = paperRecordInfoDao.deletePaperRecordInfo(id,userId);
            Suggest suggest = suggestDao.selectByPaperIdAndUserId(id,userId);
            suggest.setStatus(2);
            suggest.setUpdateTime(new Date());
            suggestDao.updateById(suggest);
            return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
